SDK for Tailslide
Installation
Install the Tailslide npm package with
- Node.js:
npm install tailslide - Go:
go get github.com/tailslide-io/tailslide.go - Python:
pip install tailslide-sdk - Node.js:
gem install tailslide
Basic Usage
Instantiating and Initializing FlagManager
The FlagManagerclass is the entry point of this SDK. It is responsible for retrieving all the flag rulesets for a given app with its appId and creating new Toggler instances to handle toggling of feature flags within that app. To instantiate a FlagManager object, a user must provide a configuration object:
- JavaScript
- Python
- Ruby
- Golang
const FlagManager = require('tailslide');
const config = {
natsServer: 'nats://localhost:4222',
natsStream: 'flags_ruleset',
appId: 1,
userContext: '375d39e6-9c3f-4f58-80bd-e5960b710295',
sdkKey: 'myToken',
redisHost: 'http://localhost',
redisPort: 6379,
};
const manager = new FlagManager(config);
await manager.initialize();
import asyncio
from tailslide import FlagManager
config = {
"nats_server": "nats://localhost:4222",
"nats_stream": "flags_ruleset",
"app_id": 1,
"user_context": "375d39e6-9c3f-4f58-80bd-e5960b710295",
"sdk_key": "myToken",
"redis_host": "http://localhost",
"redis_port": 6379,
}
async def main():
manager = FlagManager(**config)
await manager.initialize()
asyncio.run(main())
require "async"
require('tailslide')
config = {
nats_server: "nats://localhost:4222",
nats_stream: "flags_ruleset",
app_id: 1,
user_context: "375d39e6-9c3f-4f58-80bd-e5960b710295",
sdk_key: "myToken",
redis_host: "http://localhost",
redis_port: 6379,
}
Async do |task|
manager = FlagManager.new(**config)
manager.initialize_flags
end
import (
tailslide "github.com/tailslide-io/tailslide.go"
)
func main(){
config := tailslide.FlagManagerConfig{
NatsServer: "nats://localhost:4222",
NatsStream: "flags_ruleset",
AppId: "1",
UserContext: "375d39e6-9c3f-4f58-80bd-e5960b710295",
SdkKey: "myToken",
RedisHost: "http://localhost",
RedisPort: "6379",
}
manager := tailslide.NewFlagManager(config)
manager.InitializeFlags()
}
natsServeris the NATS JetStream serveraddress:portnatsStreamis the NATS JetStream’s stream name that stores all the apps and their flag rulesetsappIdis the ID number of the app the user wants to retrieve its flag ruleset fromuserContextis the UUID string that identifies the current usersdkKeyis the SDK key for the Tailslide, it is used as a password for NATS JetStream token authenticationredisHostis the address to the Redis databaseredisPortis the port number that the Redis database runs on
After instantiating a FlagManager, invoke the initialize method. This method connects the FlagManager instance to both NATS JetStream and Redis Timeseries, and asynchronously retrieves the latest and any new flag ruleset data.
Using Feature Flag with Toggler
Once the FlagManager is initialized, it can create a Toggler, with the newToggler method, for each feature flag that the developer wants to wrap the new and old features in. A Toggler’s isFlagActive method checks whether the flag with its flagName is active or not based on the flag ruleset. A Toggler’s isFlagActive method returns a boolean value, which can be used to evaluate whether a new feature should be used or not.
- JavaScript
- Python
- Ruby
- Golang
const flagConfig = {
flagName: 'App 1 Flag 1',
};
const flagToggler = manager.newToggler(flagConfig);
if (flagToggler.isFlagActive()) {
// call new feature here
} else {
// call old feature here
}
flag_config = {
"flag_name": 'App 1 Flag 1',
}
flag_toggler = manager.new_toggler(flag_config)
if flag_toggler.is_flag_active():
# call new feature here
else:
# call old feature here
flag_config = {
flag_name: 'App 1 Flag 1',
}
flag_toggler = manager.new_toggler(flag_config)
if flag_toggler.is_flag_active
# call new feature here
else
# call old feature here
end
flagConfig := tailslide.TogglerConfig{
FlagName: "App 1 Flag 1",
}
flagToggler, err := manager.NewToggler(flagConfig)
if flagToggler.IsFlagActive() {
// call new feature here
} else {
// call old feature here
}
Emitting Success or Failture
To use a Toggler instance to record successful or failed operations, call its emitSuccess or emitFailure methods:
- JavaScript
- Python
- Ruby
- Golang
if (successCondition) {
await flagToggler.emitSuccess();
} else {
await flagToggler.emitFailure();
}
if successCondition:
flag_toggler.emit_success()
else:
flag_toggler.emit_failure()
if successCondition
flag_toggler.emit_success
else
flag_toggler.emit_failure
end
if successCondition {
flagToggler.EmitSuccess()
} else {
flagToggler.EmitFailure()
}
Documentation
For language specific documentation, please see README files at the following links:
JavaScript: https://github.com/tailslide-io/tailslide.js
Ruby: https://github.com/tailslide-io/tailslide.rb